﻿#include <iostream>

#include <opencv2/opencv.hpp>

#include <pil/base/Svar/Svar.h>
#include <pil/base/Path/Path.h>
#include <pil/base/Types/VecParament.h>
#include <pil/base/Svar/Scommand.h>
#include <pil/base/Path/Path_Utils.h>
#include <pil/base/Path/Path.h>
#include <pil/base/ClassLoader/SharedLibrary.h>

#include "../VideoReader.h"

using namespace std;

int main(int argc, char *argv[])
{
    svar.ParseMain(argc,argv);
    string vrPlugInName = "libPIVideoReader";
    vrPlugInName += svar.GetString("plugInName","V4L2");         //FC CV
    vrPlugInName += ".so";
    pi::SharedLibrary         videoReaderPlugIn;
    videoReaderPlugIn.load(vrPlugInName);
    if(!videoReaderPlugIn.isLoaded())
    {
        printf("load failed!\n");
        return -1;
    }
    getVideoReaderFunction func = (getVideoReaderFunction)videoReaderPlugIn.getSymbol("getVideoReader");
    if(!func)
    {
        printf("get symbol failed!\n");
        return -2;
    }
    SPtr<pi::VideoReader> reader = func();
    if(!reader.get()){
        printf("failed to get videoReader!\n");
        return -3;
    }

    reader->open(svar.GetString("input","/dev/video0"));


    uchar key = 0;
    while (key!=27) {
        pi::VideoFrame frame;
        reader->grabImage(frame);
        cv::Mat src(frame.height,frame.width,CV_8UC3,frame.img);

        cv::imshow("PlugIn",src);
        key = cv::waitKey(10);
    }

    return 0;
}
